<?php

$order_id  = $this->getRequest()->getParam('order_id');
$vendorId  = Mage::getSingleton('vendors/session')->getVendor()->getId();



// $orderinfo->getData() = Mage::getSingleton('core/resource')->getConnection('core_read')
//             ->fetchAll("select * from tb_sales_flat_order where vendor_id = ". $vendorId ." and entity_id =". $order_id );
$orderinfo = Mage::getModel('vendorssales/order')->getCollection();
$orderinfo->getSelect()
		  ->where('vendor_id = ?', $vendorId)
		  ->where('entity_id = ?', $order_id);

// $orderaddress->getData() = Mage::getSingleton('core/resource')->getConnection('core_read')
//               ->fetchAll("select * from tb_sales_flat_order_address where parent_id = ". $order_id );
$orderaddress = Mage::getModel('vendorssales/address')->getCollection();
$orderaddress->getSelect()->where('parent_id = ?', $order_id);



// $orderitem->getData() = Mage::getSingleton('core/resource')->getConnection('core_read')
//               ->fetchAll("select * from tb_sales_flat_order_item where order_id = ". $order_id );
$orderitem = Mage::getModel('vendorssales/item')->getCollection();
$orderitem->getSelect()->where('order_id = ?', $order_id);



switch ($orderinfo->getData()[0]['v_carrier_product']) {
	case '29':
		$shipping_method = 'Ecommerce Express';
		break;
	case '9':
		$shipping_method = 'Non-registered Mail';
		break;
	case '10':
		$shipping_method = 'Registered Mail';
		break;
}
?>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="<?php echo Mage::getUrl(); ?>skin/adminhtml/default/enterprise/boxes.css" media="all" />
<link rel="shortcut icon" href="<?php echo Mage::getUrl(); ?>media/favicon/default/logo5_1.png" type="image/x-icon" />
<title>View Order</title>
<style type="text/css">
body,strong,label,td
{
	font-size: 14px;
	font-family: Helvetica, Arial, sans-serif;
}

a
{
	text-decoration: none;
}
.view_order
{
	margin:10px auto;
	width:80%;
}
address 
{
    font-style: normal;
    margin-bottom: .5em;
}
.grid tr.headings th
{
	padding: 5px;
}
.order_footer 
{
	margin-top: 20px;
	float:right;
}
.print_waybill button,.close button
{
	padding: 5px;
	margin-left: 30px;
}
</style>
</head>
<body>
<div class="view_order">
	<div class="box-left">
	    <!--Order Information-->
	    <div class="entry-edit">
	        <div class="entry-edit-head">
	            <h4 class="icon-head head-account"><?php echo Mage::helper('vendorssales')->__('Order Information') ?></h4>
	        </div>
	        <div class="fieldset">
	            <table cellspacing="0" class="form-list">
	            <tr>
	                <td class="label"><label><?php echo Mage::helper('vendorssales')->__('Order No.') ?></label></td>
	                <td class="value"><strong><?php echo $orderinfo->getData()[0]['increment_id']?></strong></td>
	            </tr>
	            <tr>
	                <td class="label"><label><?php echo Mage::helper('vendorssales')->__('Order Date') ?></label></td>
	                <td class="value"><strong><?php echo $orderinfo->getData()[0]['d_create_date']?></strong></td>
	            </tr>
	            </table>
	        </div>
	    </div>
	</div>

	<div class="box-right">
	    <!--Account Information-->
	    <div class="entry-edit">
	        <div class="entry-edit-head">
	            <h4 class="icon-head head-account"><?php echo Mage::helper('vendorssales')->__('Account Information') ?></h4>
	        </div>
	        <div class="fieldset">
	            <div class="hor-scroll">
	                <table cellspacing="0" class="form-list">
	                <tr>
	                    <td class="label"><label><?php echo Mage::helper('vendorssales')->__('Customer Name') ?></label></td>
	                    <td class="value">
	                    	<strong><?php echo $customer_data['firstname'] .' '. $customer_data['middlename'] .' '. $customer_data['lastname']?></strong>
	                    </td>
	                </tr>
	                <tr>
	                    <td class="label"><label><?php echo Mage::helper('vendorssales')->__('Email') ?></label></td>
	                    <td class="value">
	                    	<strong><?php echo $customer_data['email'] ?></strong>
	                    </td>
	                </tr>
	                </table>
	            </div>
	        </div>
	    </div>
	</div>

	<div class="clear"></div>

		<div class="box-left">
		    <!--Payment Information-->
		    <div class="entry-edit">
		        <div class="entry-edit-head">
		            <h4 class="icon-head head-billing-address"><?php echo Mage::helper('vendorssales')->__('Payment Information') ?></h4>
		        </div>
		        <fieldset>
		            <div><?php echo $orderinfo->getData()[0]['v_payment_method'] ?></div><br>
		            <div>Order was placed using USD</div>
		        </fieldset>
		    </div>
		</div>

		<div class="box-right">
		    <!--Shipping Address-->
		    <div class="entry-edit">
		        <div class="entry-edit-head">
		            <h4 class="icon-head head-shipping-address"><?php echo Mage::helper('vendorssales')->__('Shipping Address') ?></h4>
		        </div>
		        <fieldset>
		            <address>
		            	<?php 
		            		if(!empty($orderaddress->getData()[0]['middlename']))
		            		{
		            			echo $orderaddress->getData()[0]['firstname'] .' '. $orderaddress->getData()[0]['middlename'] .' '. $orderaddress->getData()[0]['lastname'] .'<br><br>';
		            		}
		            		else
		            		{
		            			echo $orderaddress->getData()[0]['firstname'] .' '. $orderaddress->getData()[0]['lastname'] .'<br><br>';
		            		}
		            		
		            		if(!empty($orderaddress->getData()[0]['company']))
	            			{
	            				echo $orderaddress->getData()[0]['company'] .'<br><br>';
	            			}

	            			if(!empty($orderaddress->getData()[0]['street']))
	            			{
	            				echo $orderaddress->getData()[0]['street'] .'<br><br>';
	            			}

	            			if(!empty($orderaddress->getData()[0]['city']))
	            			{
	            				echo $orderaddress->getData()[0]['city'] .' ';
	            			}

	            			if(!empty($orderaddress->getData()[0]['region']))
	            			{
	            				echo $orderaddress->getData()[0]['region'] .' ';
	            			}

	            			if(!empty($orderaddress->getData()[0]['v_zip_code']))
	            			{
	            				echo $orderaddress->getData()[0]['v_zip_code'] .'<br><br>';
	            			}
	            			else
	            			{
	            				echo '<br>';
	            			}

	            			if(!empty($orderaddress->getData()[0]['telephone']))
	            			{
	            				echo 'T: '. $orderaddress->getData()[0]['telephone'];
	            			}
		            	?>
		            </address>
		        </fieldset>
		    </div>
		</div>
	<div class="clear"></div>

	<div class="box-left">
		    <!--Shipping & Handling Information-->
		    <div class="entry-edit">
		        <div class="entry-edit-head">
		            <h4 class="icon-head head-billing-address"><?php echo Mage::helper('vendorssales')->__('Shipping & Handling Information') ?></h4>
		        </div>
		        <fieldset>
		        	<div>
		        		<?php echo $shipping_method?>
		       		</div>
		       		<?php if(!empty($orderinfo->getData()[0]['v_waybill_num'])): ?>
		       		<br>
					<div>
						<span>Waybill No. : <?php echo $orderinfo->getData()[0]['v_waybill_num'] ?></span>
			        </div>
			        <br>
			        <div>
			        	<a href="<?php echo $this->getUrl('ordertrackinginfo/index/orderid') . $orderinfo->getData()[0]['increment_id']?>" target="routeinfo">
			        		<button>More Detail</button>
			        	</a>
			        </div>
			    	<?php endif?>
		        </fieldset>
		    </div>
		</div>

		<div class="box-right">
		    <!--Order Totals-->
	    <div class="entry-edit">
	        <div class="entry-edit-head">
	            <h4 class="icon-head head-account"><?php echo Mage::helper('vendorssales')->__('Order Totals') ?></h4>
	        </div>
	        <div class="fieldset">
	            <table cellspacing="0" class="form-list">
	            <tr>
	                <td class="label"><label><?php echo Mage::helper('vendorssales')->__('Subtotal') ?></label></td>
	                <td class="value"><strong><?php echo 'USD '. $orderinfo->getData()[0]['grand_total']?></strong></td>
	            </tr>
	            <tr>
	                <td class="label"><label><?php echo Mage::helper('vendorssales')->__('Shipping & Handling') ?></label></td>
	                <td class="value"><strong><?php echo 'USD '. $orderinfo->getData()[0]['shipping_amount']?></strong></td>
	            </tr>
	            <tr>
	                <td class="label"><label><?php echo Mage::helper('vendorssales')->__('Total') ?></label></td>
	                <td class="value"><strong><?php echo 'USD '. $orderinfo->getData()[0]['grand_total']?></strong></td>
	            </tr>
	            </table>
	        </div>
	    </div>
		</div>
	<div class="clear"></div>

	<div class="entry-edit">
        <div class="entry-edit-head">
            <h4 class="icon-head head-products">Items Ordered</h4>
        </div>
    </div>
    <div class="grid np">
  <div class="hor-scroll">
    <table cellspacing="0" class="data order-tables">
        <colgroup>
        <col>
        <col width="1">
        <col width="1">
        <col width="1">
        <col width="1">
        <col width="1">
        </colgroup>
        <thead>
            <tr class="headings">
                <th>Product</th>
                <th><span class="nobr">Item Status</span></th>
                <th>Price</th>
                <th class="a-center">Qty</th>
                <th>Subtotal</th>
                <th class="last"><span class="nobr">Row Total</span></th>
            </tr>
        </thead>
        <tbody class="even">
        <?php foreach ($orderitem->getData() as $item): ?>
        <?php 
        	$product  = Mage::getModel('catalog/product')->loadByAttribute('sku', $item['sku']);
        	if(!$product) break;

        	/* 2016-04-28 13:45 判断productid是否是组合产品的子产品，还是单品。如果是子产品，需要获取组合产品的image。*/
			$configurable_ids  = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($product->getId());
			if(count($configurable_ids) > 0)
			{
				/* 获取子产品在主产品中label对应的图片 */
				$productConfig = Mage::getModel('catalog/product')->load($configurable_ids[0]);
				$attribute_option_id = $product->getData('common_color');
				$colorText = $product->setStoreId(0)->setData('common_color', $attribute_option_id)->getAttributeText('common_color');
				if(!empty($colorText) && !empty($_img = $productConfig->getMediaGalleryImages()->getItemByColumnValue('label',$colorText)))
				{
					$imageUrl = $_img->getUrl();
				}
				else
				{
					if($productConfig->getImage() != 'no_selection')
					{
						$imageUrl = $productConfig->getImageUrl();
					}
				}
			}
			else
			{
				if($product->getImage() != 'no_selection')
				{
					$imageUrl = $product->getImageUrl();
				}
			}
			
        ?>
            <tr class="border">
    			<td class="a-left">
                <div id="order_item_260" class="item-container">
                <?php if($product->getImage() != 'no_selection' || $productConfig->getImage() != 'no_selection'): ?>
                <img style="float: left;margin-right: 10px;" src="<?php echo $imageUrl ?>" width="100px" />
            	<?php endif;?>
                <div class="item-text" style="float: left;">
				    <h5 class="title"><span id="order_item_260_title"><?php echo $item['v_item_name'] ?></span></h5>
				    <div><strong>Vendor SKU:</strong> <?php echo $product->getVendorSku(); ?></div>
                </div>
                </div>
	            </td>
			    <td class="a-right">Ordered</td>
			    <td class="a-right"><span class="price">USD <?php echo $item['price']?></span></td>
			    <td class="a-right">
				    <table cellspacing="0" class="qty-table">
				    <tbody>
					    <tr>
					        <td><strong><?php echo $item['qty_ordered']?></strong></td>
					    </tr>
				    </tbody>
				    </table>
				</td>

			    <td class="a-right">
					<span class="price-excl-tax">
						<span class="price">USD <?php echo $item['price']?></span>
					</span>
				</td>
			    <td class="a-right last"><strong>USD <?php echo $item['qty_ordered'] * $item['price']?></strong></td>
			</tr>
		<?php endforeach;?>
		</tbody>
	</table>
  </div>
</div>
<div class="clear"></div>

<div class="order_footer">
<?php if(!empty($orderinfo->getData()[0]['v_waybill_num'])): ?>
<span class="print_waybill">
	<a href="<?php echo $this->getUrl('vendors/sales_order/printwaybill/order_id/'.$order_id)?>" target="printwaybill">
		<button>Print Waybill</button>
	</a>
</span>
<?php endif ?>
<span class="close">
	<a href="javascript:window.opener=null;window.close();">
		<button>Close</button>
	</a>
</span>
</div>
</div>
</body>
</html>

